/**
 * @Title: InviteController.java
 * @Package com.qding.park.api.web.service.controller
 * @Description: TODO
 * Copyright: Copyright (c) 2011 
 * Company:千丁互联
 * 
 * @author Comsys-Administrator
 * @date 2016年8月16日 下午2:49:26
 * @version V1.0
 */

package com.qding.park.manager.rest.controller;

import java.util.Date;
import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.qding.park.common.core.exceptions.BaseException;
import com.qding.park.common.core.exceptions.ExceptionMessage;
import com.qding.park.common.core.exceptions.business.adapter.AdapterException;
import com.qding.park.common.core.exceptions.business.manager.service.ServiceCoreException;
import com.qding.park.common.core.exceptions.business.validator.RchlValidatorException;
import com.qding.park.common.core.exceptions.system.SystemUnKnown;
import com.qding.park.common.core.validate.RchlValidateBean;
import com.qding.park.common.core.validate.RchlValidateTools;
import com.qding.park.common.utils.DateUtils;
import com.qding.park.manager.common.pojo.ParkDataResult;
import com.qding.park.manager.common.rest.domain.AnswerMsg;
import com.qding.park.manager.common.rest.domain.AnswerMsgFactoryImpl;
import com.qding.park.manager.common.rest.enums.ResponseCodeEnum;
import com.qding.park.manager.facade.core.service.DemoFacade;
import com.qding.park.manager.facade.core.service.common.InviteCommonFacade;
import com.qding.park.manager.pojo.TbInvitecard;
import com.qding.park.manager.rest.conf.SystemConfig;
import com.qding.park.manager.rest.enums.VisitTimeEnum;

/**
 * @ClassName: InviteController
 * @Description: 邀请Controller
 * @author Comsys-Administrator
 * @date 2016年8月16日 下午2:49:26
 *
 */
@RestController
@EnableAutoConfiguration
@RequestMapping("/invite")
public class InviteController extends WebBaseController{
	
	private static final Log _log = LogFactory.getLog(InviteController.class);
	
	@Autowired
	private  InviteCommonFacade  inviteCommonFacade;
	
	@Autowired
	private SystemConfig systemConfig;
	
	
	@Autowired
	private  DemoFacade  demoFacade;
	
	/**
	 * @Description (创建邀请)
	 * @param carNum 车牌
	 * @param visitTime  来访时间
	 * @param visitPurpose  来访目的
	 * @param visitAddress  访问地址
	 * @param userId  邀请人id
	 * @return
	 */
	@RequestMapping("/createInvite")
	public AnswerMsg createInvite(String carNum, String visitTime, String visitPurpose, String visitAddress,
			String userId,String parkId) {
			
		try {
			/******验证字段********/
			RchlValidateBean vb = new RchlValidateBean();
			//必填字段
			vb.getRequiredValidate().put("carNum", carNum);
			vb.getRequiredValidate().put("visitTime", visitTime);
			vb.getRequiredValidate().put("visitPurpose", visitPurpose);
//			vb.getRequiredValidate().put("visitAddress", visitAddress);
			vb.getRequiredValidate().put("userId", userId);
			vb.getRequiredValidate().put("parkId", parkId);
			
			
			//int 类型字段
			vb.getIntValidate().put("visitPurpose", visitPurpose);
			vb.getIntValidate().put("parkId", parkId);
			vb.getIntValidate().put("visitTime", visitTime);
			RchlValidateTools.validate(vb);
			/**************/
			int visitTimeType=Integer.valueOf(visitTime);
			Date startTime=null;
			Date endTime=null;
			if(VisitTimeEnum.TODAY.getCode()==visitTimeType){
				//今天
				startTime=new Date();
				endTime=DateUtils.getDayEnd(new Date());
			}else if(VisitTimeEnum.TOMORROW.getCode()==visitTimeType){
				//明天
				
				startTime=DateUtils.getDayStart(DateUtils.addDay(new Date(),1));
				endTime=DateUtils.getDayEnd(DateUtils.addDay(new Date(),1));
			}else{
				throw new RchlValidatorException(RchlValidatorException.TypeEnum.EXCEPTION_VALIDATOR.getId(), null,
						new ExceptionMessage(RchlValidatorException.TypeEnum.EXCEPTION_VALIDATOR.getDesc(),
								"","visitTime 字段非法"));
			}
			
			
			// parkFacade.getParkings(userId, phone);
			String bjuerid=userId;
			int visitPurposeInt=Integer.valueOf(visitPurpose);
			
			
			TbInvitecard tbInvitecard=new TbInvitecard();
			tbInvitecard.setCarNum(carNum);
			tbInvitecard.setStartTime(startTime);
			tbInvitecard.setEndTime(endTime);
			tbInvitecard.setVisitPurpose(visitPurposeInt);
			tbInvitecard.setVisitAddress(visitAddress);
			tbInvitecard.setBjuserid(bjuerid);
			tbInvitecard.setParkID(Integer.valueOf(parkId));
			
			List<String> demoPark=systemConfig.calculationDemoPark();
			ParkDataResult dataResult =null;
			if(demoPark.contains(parkId)){
				//演示请求
				dataResult=demoFacade.createInvite(tbInvitecard);
			}else{
				dataResult = inviteCommonFacade.createInvite(tbInvitecard);
			}
			

			AnswerMsg answerMsg = AnswerMsgFactoryImpl.createAnswerMsg(dataResult.getData());
			return answerMsg;
		}catch(RchlValidatorException e){
			
			exceptionHandle.handle(e);
			AnswerMsg answerMsg = AnswerMsgFactoryImpl.createErrorAnserMsg(ResponseCodeEnum.VALIDATION_ERROR.getValue(),
					e.getExceptionMessage().getSimpleText()+";"+e.getExceptionMessage().getDetailText());
			return answerMsg;
		} 
		catch (AdapterException e) {
			exceptionHandle.handle(e);
			AnswerMsg answerMsg = AnswerMsgFactoryImpl.createErrorAnserMsg(ResponseCodeEnum.BUSINESS_ERROR.getValue(),
					e.getExceptionMessage().getSimpleText());
			return answerMsg;
		}
		catch (BaseException e) {
			exceptionHandle.handle(e);
			AnswerMsg answerMsg = AnswerMsgFactoryImpl.createErrorAnserMsg(ResponseCodeEnum.BUSINESS_ERROR.getValue(),
					e.getExceptionMessage().getSimpleText());
			return answerMsg;
		}
		
		catch (Exception e) {
			SystemUnKnown ex = new SystemUnKnown(e,
					ExceptionMessage.createExceptionMessage(SystemUnKnown.default_Name, "未知Exception",
							"carNum=" + carNum + ",parkId=" + parkId + ",visitTime=" + visitTime + ",visitPurpose=" + visitPurpose
									+ ",userId=" + userId+",visitAddress="+visitAddress));
			exceptionHandle.handle(ex);
			AnswerMsg answerMsg = AnswerMsgFactoryImpl.createErrorAnserMsg(ResponseCodeEnum.SYSTEM_ERROR);
			return answerMsg;
		}

	}

	/**
	 * @Description (取消邀请)
	 * @param orderId  邀请单号
	 * @return
	 */
	@RequestMapping("/cancelInvite")
	public AnswerMsg cancelInvite(String orderId) {
			
		try {
			/******验证字段********/
			RchlValidateBean vb = new RchlValidateBean();
			vb.getRequiredValidate().put("orderId", orderId);
			
			RchlValidateTools.validate(vb);
			/**************/
			
			ParkDataResult dataResult =null;
			if(this.isDemoRequestByOrder(orderId)){
				//演示请求
				return AnswerMsgFactoryImpl.createAnswerMsg(ParkDataResult.createEmpty().getData());
			}else{
				dataResult = inviteCommonFacade.cancelInvite(orderId);
			}
			
			

			AnswerMsg answerMsg = AnswerMsgFactoryImpl.createAnswerMsg(dataResult.getData());
			return answerMsg;
		}catch(RchlValidatorException e){
			
			exceptionHandle.handle(e);
			AnswerMsg answerMsg = AnswerMsgFactoryImpl.createErrorAnserMsg(ResponseCodeEnum.VALIDATION_ERROR.getValue(),
					e.getExceptionMessage().getSimpleText()+";"+e.getExceptionMessage().getDetailText());
			return answerMsg;
		}catch(ServiceCoreException e){
			exceptionHandle.handle(e);
			AnswerMsg answerMsg = AnswerMsgFactoryImpl.createErrorAnserMsg(ResponseCodeEnum.BUSINESS_ERROR.getValue(),
				e.getExceptionMessage().getDetailText());
			return answerMsg;
		}
		catch (AdapterException e) {
			exceptionHandle.handle(e);
			AnswerMsg answerMsg = AnswerMsgFactoryImpl.createErrorAnserMsg(ResponseCodeEnum.BUSINESS_ERROR.getValue(),
					e.getExceptionMessage().getSimpleText());
			return answerMsg;
		}
		catch (BaseException e) {
			exceptionHandle.handle(e);
			AnswerMsg answerMsg = AnswerMsgFactoryImpl.createErrorAnserMsg(ResponseCodeEnum.BUSINESS_ERROR.getValue(),
					e.getExceptionMessage().getSimpleText());
			return answerMsg;
		}catch (Exception e) {
			SystemUnKnown ex = new SystemUnKnown(e,
					ExceptionMessage.createExceptionMessage(SystemUnKnown.default_Name, "未知Exception","orderId=" + orderId));
			exceptionHandle.handle(ex);
			AnswerMsg answerMsg = AnswerMsgFactoryImpl.createErrorAnserMsg(ResponseCodeEnum.SYSTEM_ERROR);
			return answerMsg;
		}

	}

}
